package com.grapes.view;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.grapes.R;
import com.grapes.business.CompanyRelativeBusiness;
import com.grapes.model.Company;

public class ChooseCommentCompanyActivity extends GrapesActivity {
	private String selectedCompanyId = null;
	private ExpandableListView companyListView;
	private Button selectedCompanyBtn;
	private Map<String, List<Company>> companyEnNameFirstLetterMap;
	private List<String> firstLetters;
	private static final int MSG_LOAD_COMPANY_LIST_OK = 1;
	private static final int MSG_LOAD_COMPANY_LIST_FAIL = 2;

	private Handler handler = new Handler() {
		@Override
		public void dispatchMessage(Message msg) {
			super.dispatchMessage(msg);
			switch (msg.what) {
			case MSG_LOAD_COMPANY_LIST_OK:
				dismissPorgressDialog();
				CompanyAdapter adtper=new CompanyAdapter();
				companyListView.setAdapter(adtper);
				break;
			case MSG_LOAD_COMPANY_LIST_FAIL:
				dismissPorgressDialog();
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.choose_comment_company);
		companyListView = (ExpandableListView) findViewById(R.id.company_choose_list);
		companyListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener()
		{
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				String key=firstLetters.get(groupPosition);
				selectedCompanyId=companyEnNameFirstLetterMap.get(key).get(childPosition).getCompanyId();
				selectedCompanyBtn.setVisibility(View.VISIBLE);
				selectedCompanyBtn.setText(companyEnNameFirstLetterMap.get(key).get(childPosition).getCompanyNameCn());
				return true;
			}			
		});
		Button cancenBtn = (Button) findViewById(R.id.company_choose_title_cancel_btn);
		cancenBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				cancelSelected();
			}
		});

		Button selctedFinishedBtn = (Button) findViewById(R.id.company_choose_title_selected_btn);
		selctedFinishedBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				selectedFinished();
			}
		});
		selectedCompanyBtn=(Button) findViewById(R.id.company_choose_selected_group_btn);
		selectedCompanyBtn.setClickable(false);
		
		showProgressDialog();
		loadCompanies();
	}

	private void loadCompanies() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					List<Company> storedCompanies = CompanyRelativeBusiness
							.getInstance().getStoredCompanies();
					Collections.sort(storedCompanies,
							new CompanyNameComparator());
					companyEnNameFirstLetterMap = new HashMap<String, List<Company>>();
					firstLetters=new LinkedList<String>();
					for (int index = 0; index < storedCompanies.size(); index++) {
						String firstLetter = storedCompanies.get(index)
								.getCompanyNameEn().substring(0, 1);
						if (companyEnNameFirstLetterMap
								.containsKey(firstLetter))
							companyEnNameFirstLetterMap.get(firstLetter).add(
									storedCompanies.get(index));
						else {
							List<Company> companies = new LinkedList<Company>();
							companies.add(storedCompanies.get(index));
							companyEnNameFirstLetterMap.put(firstLetter,
									companies);
							firstLetters.add(firstLetter);
						}
					}
					Message msg = new Message();
					msg.what = MSG_LOAD_COMPANY_LIST_OK;
					handler.sendMessage(msg);
				} catch (Exception ex) {
					Message msg = new Message();
					msg.what = MSG_LOAD_COMPANY_LIST_FAIL;
					handler.sendMessage(msg);
				}
			}
		}).start();
	}

	private void cancelSelected() {
       goBack();
	}

	private void selectedFinished() {
       
	}

	class CompanyNameComparator implements Comparator<Company> {
		@Override
		public int compare(Company lhs, Company rhs) {
			return lhs.getCompanyNameEn().compareToIgnoreCase(
					rhs.getCompanyNameEn());
		}
	}

	class CompanyAdapter extends BaseExpandableListAdapter {

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return childPosition;
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {			
			TextView textView=new TextView(ChooseCommentCompanyActivity.this);
			Company company=companyEnNameFirstLetterMap.get(firstLetters.get(groupPosition)).get(childPosition);
			textView.setText(company.getCompanyNameCn());
			textView.setBackgroundResource(R.color.transparent);
			return textView;
		}

		@Override
		public int getChildrenCount(int groupPosition) {			
			return companyEnNameFirstLetterMap.get(firstLetters.get(groupPosition)).size();
		}

		@Override
		public Object getGroup(int groupPosition) {
			return groupPosition;
		}

		@Override
		public int getGroupCount() {
			return firstLetters.size();
		}

		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			TextView textView=new TextView(ChooseCommentCompanyActivity.this);
			textView.setText(firstLetters.get(groupPosition));
			textView.setBackgroundResource(R.color.transparent);
			return textView;
		}

		@Override
		public boolean hasStableIds() {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return false;
		}

	}

}
